//
//  SRSpeakingTestScoreHeaderView.m
//  StoryShow
//
//  Created by CyonLeuMBP on 2017/10/10.
//  Copyright © 2017年 qudiandu.cn. All rights reserved.
//

#import "SRSpeakingTestScoreHeaderView.h"
#import "SREvaluationWordScoreModel.h"
#import <NSString+Size.h>

@interface SRSpeakingTestScoreHeaderView ()
@property (weak, nonatomic) IBOutlet UILabel *fluencyLabel;
@property (weak, nonatomic) IBOutlet UILabel *integrityLabel;
@property (weak, nonatomic) IBOutlet UILabel *accuracyLabel;


@property (weak, nonatomic) IBOutlet UIView *wordContentView;
@property (weak, nonatomic) IBOutlet UILabel *indexLabel;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;

@property (strong, nonatomic) NSMutableArray *tagViewArray;
@property (strong, nonatomic) NSMutableArray *items;
@property (assign, nonatomic) CGFloat wordHeight;

@end

@implementation SRSpeakingTestScoreHeaderView

- (void)awakeFromNib {
	[super awakeFromNib];
	
	self.backgroundColor = [UIColor colorWithHex:0xefe6de];
	self.wordContentView.backgroundColor = [UIColor colorWithHex:0xefe6de];
	
	FZStyleSheet *css = [FZStyleSheet currentStyleSheet];
	self.fluencyLabel.textColor = self.integrityLabel.textColor = self.accuracyLabel.textColor = css.color_3;
	self.fluencyLabel.font  = self.integrityLabel.font = self.accuracyLabel.font = css.fontOfH4;
	
	self.indexLabel.textColor = css.color_3;
	self.indexLabel.font = css.fontOfH6;
	self.countLabel.textColor = [UIColor colorWithHex:0x372e26];
	self.countLabel.font = css.fontOfH6;
	
	self.items = [NSMutableArray array];
	self.tagViewArray = [NSMutableArray array];
	
	self.wordHeight = 108;//200-108=92
}

- (void)setIndex:(NSInteger)index {
	_index = index;
	
	self.indexLabel.text = [NSString stringWithFormat:@"%d", (index+1)];

}

- (void)setCount:(NSInteger)count {
	self.countLabel.text = [NSString stringWithFormat:@"/%d", count];
}

- (void)updateCellInfoForScore:(SREvaluationScoreModel *)scoreModel {
	NSNumber *fluencyScore = scoreModel.fluencyScore;
	self.fluencyLabel.text = [NSString stringWithFormat:@"流畅度：%ld", MAX(0, [fluencyScore integerValue])];
	
	NSNumber *integrityScore = scoreModel.integrityScore;
	self.integrityLabel.text = [NSString stringWithFormat:@"完整度：%ld", MAX(0, [integrityScore integerValue])];
	
	NSNumber *accuracyScore = scoreModel.accuracyScore;
	self.accuracyLabel.text = [NSString stringWithFormat:@"标准度：%ld", MAX(0, [accuracyScore integerValue])];
	[self.wordContentView removeAllSubviews];
	[self.tagViewArray removeAllObjects];
	
	if (scoreModel.wordScoreArray) {
		[self.items removeAllObjects];
		[self.items addObjectsFromArray:scoreModel.wordScoreArray];
		[self layoutTagsView];
		
	} else {
		
	}
}


- (void)layoutTagsView {
	
	CGFloat itemSpace = 10;
	CGFloat edgeSpace = 15;
	
	CGFloat itemHeight = 25;
	CGFloat allWidth = SCREEN_WIDTH - 30;
	CGFloat leftWidth = allWidth;
	CGFloat leftX = edgeSpace;
	CGFloat topY = itemSpace;
	CGFloat allHeight = 0;
	
	NSInteger row = 1;
	FZStyleSheet *css = [FZStyleSheet currentStyleSheet];
	
	for (int i = 0; i < [self.items count] ; ++ i) {
		UILabel *tagLabel = [[UILabel alloc] init];
		tagLabel.textColor = [UIColor colorWithHex:0x372e26];
		tagLabel.font = css.fontOfH6;
		tagLabel.textAlignment = NSTextAlignmentCenter;
		tagLabel.layer.cornerRadius = itemHeight/2.0;
		tagLabel.clipsToBounds = YES;
		tagLabel.layer.borderColor = css.color_7.CGColor;
		tagLabel.layer.borderWidth = 1;
		[self.wordContentView addSubview:tagLabel];
		tagLabel.tag = i;
		
		[self.tagViewArray addObject:tagLabel];
		
//		WEAKSELF
//		[tagLabel addTapGestureHandler:^(UITapGestureRecognizer *gestureRecognizer, UIView *view, CGPoint locationInView) {
//			NSInteger tagIndex = view.tag;
//			UILabel *tagSelectLabel = (UILabel *)view;
//
//			if (tagIndex >= 0 && tagIndex < [weakSelf.items count]) {
//				SREvaluationWordScoreModel *selectItem = weakSelf.items[tagIndex];
//				tagSelectLabel.textColor = [FZStyleSheet currentStyleSheet].color_6;
//				tagSelectLabel.layer.borderColor = [FZStyleSheet currentStyleSheet].color_7.CGColor;
//			}
//		}];
		
		SREvaluationWordScoreModel *item = self.items[i];
		NSString *title = [NSString stringWithFormat:@"%@  %@", item.word, item.score];
		tagLabel.text = title;
		NSInteger score = [item.score integerValue];
		if (score >= 60) {
			tagLabel.layer.borderColor = [UIColor colorWithHex:0x5CC323].CGColor;
		} else if(score > 10) {
			tagLabel.layer.borderColor = [UIColor colorWithHex:0xfd4f1a].CGColor;
		} else {
			//没有识别 0分
			tagLabel.layer.borderColor = [UIColor colorWithHex:0xc4bab1].CGColor;
		}
		
		
		CGSize textSize = [title textSizeWithFont:css.fontOfH4 constrainedToSize:CGSizeMake(1000, 20) lineBreakMode:NSLineBreakByWordWrapping];
		CGFloat itemWidth = textSize.width + 20;
		if (itemWidth <= leftWidth) {
			tagLabel.frame = CGRectMake(leftX, topY, itemWidth, itemHeight);
			leftWidth -= itemWidth + itemSpace;
			leftX += itemWidth + itemSpace;
			
		} else {
			leftWidth = allWidth;
			leftX = edgeSpace;
			
			topY = row * (itemHeight + itemSpace) + itemSpace;
			
			tagLabel.frame = CGRectMake(leftX, topY, itemWidth, itemHeight);
			leftWidth -= itemWidth + itemSpace;
			leftX = itemWidth + edgeSpace + itemSpace;
			
			++ row;
			
		}
	}
	
	allHeight = row * (itemHeight + itemSpace) + edgeSpace;
	
	self.wordHeight = allHeight;
	
	if (self.didChangeWordHeightBlock) {
		self.didChangeWordHeightBlock(self.wordHeight);
	}
}


@end
